<?php
/**
 * @package site
 */
class Package extends DataObject implements PermissionProvider {
    private static $singular_name = "Package";
    private static $plural_name = "Packages";

    private static $db = array(
        'IsActive' => 'Boolean',
        'Type' => "Enum(array('Register', 'Upgrade'))",
        'Code' => 'Varchar',
        'Title' => 'Varchar(250)',
        'Sort' => 'Int'
    );

    private static $has_one = array(
        'UpgradeGroup' => 'Group'
    );

    private static $many_many = array(
        'RestrictGroups' => 'Group',
        'Prices' => 'PackagePrice'
    );

    private static $defaults = array('IsActive' => 1);

    private static $default_sort = "Type, Sort";

    private static $searchable_fields = array(
        'IsActive',
        'Type',
        'Code',
        'UpgradeGroupID'
    );

    private static $summary_fields = array(
        'IsActive.Nice',
        'Type',
        'Code',
        'Title',
        'UpgradeGroup.TitleByLang',
        'RestrictGroupsText'
    );

    public function fieldLabels($includerelations = true) {
        $labels = parent::fieldLabels($includerelations);

        $labels['IsActive'] = _t('Package.IS_ACTIVE', 'Is Active?');
        $labels['IsActive.Nice'] = _t('Package.IS_ACTIVE', 'Is Active?');
        $labels['Type'] = _t('Package.TYPE', 'Type');
        $labels['Code'] = _t('Package.CODE', 'Code');
        $labels['Title'] = _t('Package.TITLE', 'Title');
        $labels['Prices'] = _t('Package.PRICES', 'Prices');
        $labels['UpgradeGroup'] = _t('Package.UPGRADE_GROUP', 'Upgrade Group');
        $labels['UpgradeGroupID'] = _t('Package.UPGRADE_GROUP', 'Upgrade Group');
        $labels['UpgradeGroup.Title'] = _t('Package.UPGRADE_GROUP', 'Upgrade Group');
        $labels['UpgradeGroup.TitleByLang'] = _t('Package.UPGRADE_GROUP', 'Upgrade Group');
        $labels['RestrictGroups'] = _t('Package.RESTRICTED_GROUP', 'Restricted Groups');
        $labels['RestrictGroupsText'] = _t('Package.RESTRICTED_GROUP', 'Restricted Groups');

        return $labels;
    }

	function getDefaultSearchContext() {
		$search_context = parent::getDefaultSearchContext();
		$groups = Group::get()->filter('IsDistributorGroup', 1)->sort('Sort')->map('ID', 'TitleByLang')->toArray();
		$search_context->getSearchFields()->dataFieldByName('UpgradeGroupID')->setSource($groups);
		return $search_context;
  	}

    function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->dataFieldByName('UpgradeGroupID')->setSource(Group::get()->filter('IsDistributorGroup', 1)->sort('Sort')->map('ID', 'TitleByLang')->toArray());
        $fields->removeByName('RestrictGroups');
        $fields->addFieldToTab('Root.Main', ListboxField::create('RestrictGroups', $this->fieldLabel('RestrictGroups'), Group::get()->filter('IsDistributorGroup', 1)->sort('Sort')->map('ID', 'TitleByLang')->toArray())->setMultiple(true));

        if($this->exists()) {
			$fields->dataFieldByName('Prices')->getConfig()->removeComponentsByType('GridFieldAddExistingAutocompleter')->removeComponentsByType('GridFieldDeleteAction')->removeComponentsByType('GridFieldEditButton')->addComponent(new GridFieldManyRelationHandler(false), 'GridFieldPaginator');
        }
		
		$fields->removeByName('Sort');

        return $fields;
    }

    function RestrictGroupsText() {
        return implode(', ', $this->RestrictGroups()->sort('Sort')->map('ID', 'TitleByLang')->toArray());
    }

    function getTitle() {
        return $this->UpgradeGroup()->TitleByLang;
    }

    function getTitleByLang() {
        return _t(sprintf('Package.%s', strtoupper($this->Code)), $this->Title);
    }

    function canView($member = false) {
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('VIEW_Package');
    }

    function canEdit($member = false) {
    	if(!$this->exists() && $this->canCreate($member)){
    		return true;
    	}
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('EDIT_Package');
    }

    function canDelete($member = false) {
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('DELETE_Package');
    }

    function canCreate($member = false) {
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('CREATE_Package');
    }

    public function providePermissions() {
        return array(
            'VIEW_Package' => array(
                'name' => _t('Package.PERMISSION_VIEW', 'Allow view access right'),
                'category' => _t('Package.PERMISSIONS_CATEGORY', 'Package')
            ),
            'EDIT_Package' => array(
                'name' => _t('Package.PERMISSION_EDIT', 'Allow edit access right'),
                'category' => _t('Package.PERMISSIONS_CATEGORY', 'Package')
            ),
            'DELETE_Package' => array(
                'name' => _t('Package.PERMISSION_DELETE', 'Allow delete access right'),
                'category' => _t('Package.PERMISSIONS_CATEGORY', 'Package')
            ),
            'CREATE_Package' => array(
                'name' => _t('Package.PERMISSION_CREATE', 'Allow create access right'),
                'category' => _t('Package.PERMISSIONS_CATEGORY', 'Package')
            )
        );
    }

}
?>
